/*******************************************************************************
 * Copyright (C) 2013 Jeremy Brodt and Kevin Gillespie
 * Description: Wheel Encoder Interface
 *         $Id: encoder.h 46 2014-04-22 04:40:46Z brodtj1212@gmail.com $
 *******************************************************************************
 */

#ifndef _ENCODER_H_
#define _ENCODER_H_

#include <stdint.h> 
#include "car.h"

/***** Definitions *****/

#define TICKS_PER_MOTOR_REV   (3)
#define GEAR_RATIO            (50)
#define TICKS_PER_REV         (TICKS_PER_MOTOR_REV * GEAR_RATIO)

#define NO_ALARM              ((uint32_t)0x7FFFFFFFUL)

/***** Global Data *****/
extern volatile int32_t odometer[NUM_WHEELS];
extern volatile unsigned int valid_cnt[NUM_WHEELS];
extern int32_t alarm_count[NUM_WHEELS];
extern volatile unsigned int direction[NUM_WHEELS];
extern const uint16_t hundred_mm_per_cnt[NUM_WHEELS];
extern const uint16_t mms7_per_cnt[NUM_WHEELS];

/***** Function Prototypes *****/
int enc_init(void);
unsigned int enc_get_speed(unsigned int wheel);
void enc_display(unsigned int wheel);
void enc_alarm_set(unsigned int wheel, int32_t count, void (*func)(void));
void enc_alarm_clr(unsigned int wheel);
#define enc_alarm_remaining(wheel)  ((alarm_count[wheel] == NO_ALARM) ? 0 : (alarm_count[wheel] - odometer[wheel]))
void enc_direction_change(unsigned int wheel, unsigned int new_direction);
void enc_log_clear(void);
void enc_log_display(void);
void enc_log_display_first(void);
void enc_log_display_recent(void);
#define enc_is_speed_valid()        ( (valid_cnt[LEFT] > 6) && (valid_cnt[RIGHT] > 6) )

#endif /* _ENCODER_H_ */
